//
//  XFDownloadDemoTests.swift
//  XFDownloadDemoTests
//
//  Created by mac on 2018/11/5.
//  Copyright © 2018 zeroLu. All rights reserved.
//

import XCTest
@testable import XFDownloadDemo

class XFDownloadDemoTests: XCTestCase {

    let urlStrings = ["http://bcm3.oss-cn-shenzhen.aliyuncs.com/prod/course/section/video/2018-07-09/db8384c8bb7ca255df562f0936b5da36.mp4",
                      "http://bcm3.oss-cn-shenzhen.aliyuncs.com/prod/course/section/video/2018-07-09/da53c83676cbdce091739c71a8bb0f5d.mp4",
                      "http://bcm3.oss-cn-shenzhen.aliyuncs.com/prod/course/section/video/2018-07-04/23d244339515668601684dfb496ffcdd.mp4",
                      "http://bcm3.oss-cn-shenzhen.aliyuncs.com/prod/course/section/video/2018-07-04/6c9fc235f054b9613bf11f9a077726ac.mp4"]
    
    override func setUp() {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }

    func testMaxDownloadQueue() {
        
        var states = [(url: URL?, state: XFDownloadState)]()
        
        XFDownloadManager.shared.maxConcurrentCount = 1
        
        for urlString in urlStrings {
            XFDownloadManager.shared.download(url: URL(string: urlString), stateBlock: { (currentUrl, state) in
                states.append((url: currentUrl, state: state))
                
            }, progressBlock: { (currentUrl, receiveSize, totalSize, progress) in
                
            }) { (finishUrl, isSuccess, sandboxPath, error) in
                
            }
        }
        
        let downloadings = states.filter({ $0.state == XFDownloadState.running })
        
        XCTAssertEqual(downloadings.count, 1, "正在下载数大于限制数")
    }
    
    func testAllBeginToAllSuspend() {
        
        var states = [(url: URL?, state: XFDownloadState)]()
        
        XFDownloadManager.shared.maxConcurrentCount = -1
        
        for urlString in urlStrings {
            XFDownloadManager.shared.download(url: URL(string: urlString), stateBlock: { (currentUrl, state) in
                states.append((url: currentUrl, state: state))
            }, progressBlock: { (currentUrl, receiveSize, totalSize, progress) in
                
            }) { (finishUrl, isSuccess, sandboxPath, error) in
                
            }
        }
        
        let downloadings = states.filter({ $0.state == XFDownloadState.running })
        
        XCTAssertEqual(downloadings.count, urlStrings.count, "正在下载的任务数不等于总的任务数")
        
        XFDownloadManager.shared.suspendDownloadAll()
        
        let suspends = states.filter({ $0.state == XFDownloadState.suspended })
        
        XCTAssertEqual(suspends.count, urlStrings.count, "已经暂停下载任务数不等于总的任务数")
        
        states.removeAll()
        
        for urlString in urlStrings {
            XFDownloadManager.shared.autoBeginOrSuspend(URL(string: urlString))
        }
        
        let resumes = states.filter({ $0.state == XFDownloadState.running })
        
        XCTAssertEqual(resumes.count, urlStrings.count, "恢复下载任务数不等于总的任务数")
        
    }

}
